package com.demo.web;

import javax.servlet.http.HttpServletRequest;

import com.demo.entity.MeUser;
import com.demo.entity.MeUserLog;
import com.demo.entity.enu.DoTypeEnum;
import com.demo.server.RegisterServer;
import com.demo.server.UserLogServer;
import com.demo.web.base.BaseModel;
import com.jweb.annotaion.JAutowired;
import com.jweb.annotaion.JController;
import com.jweb.core.handle.Controller;
import com.jweb.util.FormatUtil;
import com.jweb.util.WebUtil;

@JController("/register")
public class RegisterController extends Controller {

	@JAutowired
	public RegisterServer server;
	public @JAutowired UserLogServer ulogServer;

	public void RegisterManager() {
	}

	/**
	 * 执行注册操作Action
	 */
	public void ajaxRegisterAction(MeUser user, HttpServletRequest req) {
		System.err.println(FormatUtil.toJSONString(user));
		System.err.println(FormatUtil.toJSONString(getParameterMap()));
		String validate = getParameter("validate");
		String validate2 = (String) WebUtil.getSessionAttribute(req, "ValidateCode");
		if (!validate.equalsIgnoreCase(validate2))
			toWriterJSON(new BaseModel(BaseModel.ERROR, "验证码输入不正确"));
		else if (server.add(user) > 0) {
			ulogServer.asynAddUserLog(req, DoTypeEnum.REGISTER, user.getUserId());
			toWriterJSON(new BaseModel(BaseModel.OK, "恭喜注册成功啦！赶紧登录体验吧~♪(´▽｀)"));
		} else
			toWriterJSON(new BaseModel(BaseModel.ERROR, "新增出现异常！"));
	}

	/**
	 * 别名和登录名校验
	 */
	public void ajaxCheckName() {
		if (server.getByLoginName(getParameter("rLoginName")) != null)
			toWriterJSON(new BaseModel(BaseModel.ERROR, "rLoginName"));
		else if (server.getByAliasName(getParameter("rAliasName")) != null)
			toWriterJSON(new BaseModel(BaseModel.ERROR, "rAliasName"));
		else
			toWriterJSON(new BaseModel());
	}
}
